package com.ruoyi.StarStudio.MQTT;

import com.ruoyi.StarStudio.MQTT.Config.MqttConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Component
public class Init {
    @Autowired
    private  EmqClient emqClient;

    @Autowired
    private MqttConfig mqttConfig;

    @PostConstruct
    public void init(){
        //连接服务端
        emqClient.connect(mqttConfig.getUsername(), mqttConfig.getPassword());
        //订阅主题，每个传感器对应一个主题，并将数据保存到数据库(回调方法中执行)
        emqClient.subscribe("SoilPH_P", QosEnum.Qos2);
        emqClient.subscribe("Meteorology_P", QosEnum.Qos2);
        emqClient.subscribe("SoilHumiture_P", QosEnum.Qos2);
        emqClient.subscribe("Precipitation_P", QosEnum.Qos2);
        emqClient.subscribe("WindDirection_P", QosEnum.Qos2);
        emqClient.subscribe("WindSpeed_P", QosEnum.Qos2);
    }
}
